package intext;

import com.adxon.MsgException;
import com.adxon.data2.Base;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.*;

@SuppressWarnings("serial")
public class SetupServlet extends HttpServlet{
	public static void main(String[] args){
		new SetupServlet().setup();
		System.out.println("Ok.");
	}
	public void doGet(HttpServletRequest req,HttpServletResponse resp)
			throws IOException, ServletException{
		System.out.println("SetupServlet");
		setup();
		resp.sendRedirect("/");
	}
	private void setup(){
		try{
			Connection c=Base.getConn();
			Statement s=c.createStatement(); 
			s.executeUpdate("DROP TABLE IF EXISTS k");
			s.executeUpdate("CREATE TABLE k("+
					"id INT NOT NULL AUTO_INCREMENT COMMENT 'id',"+
					"PRIMARY KEY(id))");
			s.executeUpdate("DROP TABLE IF EXISTS i");
			s.executeUpdate("CREATE TABLE i("+
					"id BIGINT NOT NULL COMMENT 'id',"+
					"at BIGINT NOT NULL COMMENT 'attachment id',"+
					"ds MEDIUMTEXT NULL COMMENT 'description',"+
					"kn INT NOT NULL COMMENT 'kind',"+
					"ow BIGINT NOT NULL COMMENT 'owner id',"+
					"ph BIGINT NOT NULL COMMENT 'path id',"+
					"rc INT NOT NULL COMMENT 'ref count',"+
					"rf BIGINT NOT NULL COMMENT 'ref id',"+
					"rt TINYINT NOT NULL COMMENT 'rate',"+
					"ta BIGINT NOT NULL COMMENT 'access time',"+
					"tc BIGINT NOT NULL COMMENT 'create time',"+
					"tg TEXT NULL COMMENT 'tags',"+
					"tm BIGINT NOT NULL COMMENT 'modify time',"+
					"tx VARCHAR(255)NULL COMMENT 'text',"+
					"xt MEDIUMTEXT NULL COMMENT 'extra',"+
					"PRIMARY KEY(id),"+
					"INDEX(tm))");
			s.executeUpdate("DROP TABLE IF EXISTS i5");
			s.executeUpdate("CREATE TABLE i5("+
					"id BIGINT NOT NULL COMMENT 'id',"+
					"ba MEDIUMBLOB NOT NULL COMMENT 'Extension',"+
					"bc MEDIUMINT NOT NULL COMMENT 'length of bits',"+
					"bo MEDIUMBLOB NOT NULL COMMENT 'blob',"+
					"bt MEDIUMBLOB NOT NULL COMMENT 'bits',"+
					"ow BIGINT NOT NULL COMMENT 'owner id',"+
					"rc INT NOT NULL COMMENT 'ref count',"+
					"rt TINYINT NOT NULL COMMENT 'rate',"+
					"tc BIGINT NOT NULL COMMENT 'create time',"+
					"tm BIGINT NOT NULL COMMENT 'modify time',"+
					"PRIMARY KEY(id),"+
					"INDEX(tm))");
			s.executeUpdate("DROP TABLE IF EXISTS i6");
			s.executeUpdate("CREATE TABLE i6("+
					"id BIGINT NOT NULL COMMENT 'id',"+
					"kn INT NOT NULL COMMENT 'kind',"+
					"ow BIGINT NOT NULL COMMENT 'owner id',"+
					"rt TINYINT NOT NULL COMMENT 'rate',"+
					"tc BIGINT NOT NULL COMMENT 'create time',"+
					"tg TEXT NOT NULL COMMENT 'tags',"+
					"tm BIGINT NOT NULL COMMENT 'modify time',"+
					"PRIMARY KEY(id),"+
					"INDEX(tm))");
			s.executeUpdate("DROP TABLE IF EXISTS i7");
			s.executeUpdate("CREATE TABLE i7("+
					"id VARCHAR(64)NOT NULL COMMENT 'id',"+
					"ow BIGINT NOT NULL COMMENT 'owner id',"+
					"tc BIGINT NOT NULL COMMENT 'create time',"+
					"tm BIGINT NOT NULL COMMENT 'modify time',"+
					"PRIMARY KEY(id),"+
					"INDEX(tm))");
			s.executeUpdate("DROP TABLE IF EXISTS i9");
			s.executeUpdate("CREATE TABLE i9("+
					"id BIGINT NOT NULL COMMENT 'id',"+
					"kn INT NOT NULL COMMENT 'kind',"+
					"ow BIGINT NOT NULL COMMENT 'owner id',"+
					"rt TINYINT NOT NULL COMMENT 'rate',"+
					"tc BIGINT NOT NULL COMMENT 'create time',"+
					"tg VARCHAR(255)NOT NULL COMMENT 'tags',"+
					"tm BIGINT NOT NULL COMMENT 'modify time',"+
					"PRIMARY KEY(id,ow,tg))");
			s.executeUpdate("DROP TABLE IF EXISTS i11");
			s.executeUpdate("CREATE TABLE i11("+
					"id BIGINT NOT NULL COMMENT 'id',"+
					"pw VARCHAR(255)NOT NULL COMMENT 'tags',"+
					"rt TINYINT NOT NULL COMMENT 'rate',"+
					"sl VARCHAR(255)NOT NULL COMMENT 'tags',"+
					"tc BIGINT NOT NULL COMMENT 'create time',"+
					"tm BIGINT NOT NULL COMMENT 'modify time',"+
					"tx VARCHAR(255)NOT NULL COMMENT 'tags',"+
					"PRIMARY KEY(tx))");
			s.close();
			c.close();
		}catch(MsgException e){
			e.printStackTrace();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
}